﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core
{
    public static partial class problem
    {
        public static dynamic addOrEditProblem(
            int? oldPid, int pid, string title, int timeLimit, int memoryLimit, bool disabled,
            string description, string input, string output, string sampleInput, string sampleOutput, string hint, string source,
            int? exampleLang, string exampleCode, string inputData, string outputData,
            bool specialJudge, int? specialLang, string specialCode)
        {
            var sql = new Sql.SqlDataContext();

            Sql.Problem p;
            if (oldPid == null) // Add
            {
                p = sql.Problem.FirstOrDefault(row => row.pid == pid);
                if (p != null) return new
                {
                    success = false,
                    msg = "PID已存在"
                };
                p = new Sql.Problem();
                p.addTime = DateTime.Now;

                var ps = new Sql.ProblemStatistics { pid = pid };
                ps.accept = ps.ce = ps.mle = ps.ole = ps.pe = ps.re = ps.se = ps.submit = ps.tle = ps.wa = 0;
                sql.ProblemStatistics.InsertOnSubmit(ps);
            }
            else // Modify
            {
                p = sql.Problem.FirstOrDefault(row => row.pid == oldPid);
                if (p == null) return new
                {
                    success = false,
                    msg = "题目不存在"
                };
                if (pid != oldPid) // PID changed
                {
                    // Update PS
                    foreach (var ps in sql.ProblemStatistics.Where(row => row.pid == oldPid))
                        ps.pid = pid;
                    // Update Status
                    foreach (var s in sql.Status.Where(row => row.pid == oldPid))
                        s.pid = pid;
                    // Update Solve
                    foreach (var so in sql.Solve.Where(row => row.pid == oldPid))
                        so.pid = pid;
                    // Update Discuss
                    foreach (var d in sql.Discuss.Where(row => row.pid == oldPid))
                        d.pid = pid;
                }
            }
            p.pid = pid;
            p.title = title;
            p.timeLimit = timeLimit;
            p.memoryLimit = memoryLimit;
            p.description = description;
            p.input = input;
            p.output = output;
            p.sampleInput = sampleInput;
            p.sampleOutput = sampleOutput;
            p.hint = hint;
            p.source = source;
            p.exampleLang = exampleLang;
            p.exampleCode = exampleCode;
            p.inputData = inputData;
            p.outputData = outputData;
            p.specialJudge = specialJudge;
            p.specialLang = specialLang;
            p.specialCode = specialCode;
            p.disabled = disabled;
            if (oldPid == null) sql.Problem.InsertOnSubmit(p);
            sql.SubmitChanges();
            return new
            {
                success = true
            };
        }
    }
}
